package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 向mogodb中添加好友关系,删除好友关系
 * 记录用户之间的关系
 */
@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public String saveUsers(Long userId, Long friendId) {
        //判断两个id是不是空
        if(!ObjectUtil.isAllNotEmpty(userId,friendId)){
            return null;
        }


        //判断是不是好友
        Query query=Query.query(Criteria.where("userId").is(userId)
        .and("friendId").is(friendId));
        long count = this.mongoTemplate.count(query, Users.class);
        if(count>0){
            //说明已经是好友关系
            return null;
        }

        Users users = new Users();

        users.setId(ObjectId.get());
        users.setDate(System.currentTimeMillis());
        users.setUserId(userId);
        users.setFriendId(friendId);

        //注册我与好友的关系
        this.mongoTemplate.save(users);

        //注册好友与我的关系
        users.setId(ObjectId.get());
        users.setUserId(friendId);
        users.setFriendId(userId);

        this.mongoTemplate.save(users);


        return users.getId().toHexString();
    }

    @Override
    public Boolean removeUsers(Long userId, Long friendId) {

        Query query1=Query.query(Criteria.where("userId").is(userId)
                .and("friendId").is(friendId));
        long deletedCount1 = this.mongoTemplate.remove(query1, Users.class).getDeletedCount();


        Query query2=Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(userId));
        long deletedCount2 = this.mongoTemplate.remove(query2, Users.class).getDeletedCount();


        return deletedCount1>0 && deletedCount2>0;
    }

    /**
     * 根据用户id查询所有用户封装到list集合
     * @param userId
     * @return
     */
    @Override
    public List<Users> queryAllUsersList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, Users.class);
    }


    /**
     * 根据用户id分页查询
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersList(Long userId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = Query.query(Criteria.where("userId").is(userId)).with(pageRequest);

        List<Users> usersList = this.mongoTemplate.find(query, Users.class);

        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);
        return pageInfo;
    }
}
